package com.rla.controller;

import cn.hutool.core.util.StrUtil;
import com.rla.common.annotation.Anonymous;
import com.rla.common.constant.Constants;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.R;
import com.rla.common.wechat.bo.WxBindingPhoneBo;
import com.rla.common.wechat.vo.WeChatMiniAuthorizeVo;
import com.rla.domain.IntegralTask;
import com.rla.domain.bo.VipUserInfoBo;
import com.rla.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/login")
public class LoginController extends BaseController {

    private final LoginService loginService;

    /*
    * 根据微信code获取手机号
    * */
    @Anonymous
    @PostMapping("/getPhoneByxcxCode")
    public R<Map<String, Object>> xcxCodeGetPhone(@RequestParam String xcxCode) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String phone = loginService.getPhoneByxcxCode(xcxCode);
        if(StrUtil.isNotBlank(phone)&&phone.length()==11){
            String showPhone =  phone.substring(0, 3) + "****" + phone.substring(7, 11);
            ajax.put("showPhone",showPhone);
            ajax.put("phone",phone);
        }
        return R.ok(ajax);
    }

    /*
    * 根据当前xcxCode获取sessionkey openid
    * */
    @Anonymous
    @PostMapping("/getxcxAuthorizeVo")
    public R<WeChatMiniAuthorizeVo> xcxgetAuthorizeVo(@RequestParam String xcxCode) {
        WeChatMiniAuthorizeVo vo  = loginService.getxcxAuthorizeVo(xcxCode);
        return R.ok(vo);
    }


    /*
     *  小程序调起手机号码进行快捷登录
     * */
    @Anonymous
    @PostMapping("/xcxQuickLogin")
    public R<Map<String, Object>> xcxQuickLogin(@Validated @RequestBody WxBindingPhoneBo bo) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.xcxQuickLogin(bo);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

//    @PostMapping("/integrate")
//    public R<VipUserInfoBo> integrate(
//            @RequestParam String level,
//            @RequestBody VipUserInfoBo vipUserInfoBo) {
//        return R.ok(loginService.operIntegrate(level,vipUserInfoBo));
//    }

    /*
    * 模拟账号登录
    * */
    @Anonymous
    @PostMapping("/simulationLogin")
    public R<Map<String, Object>> simulationLogin( @RequestBody WxBindingPhoneBo bo) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.xcxPhoneLogin(bo.getPhone(),bo.getOpenid());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

}
